<script setup lang="ts">
import {getMessage, getRead} from "@/api/home/index";
import {ref} from 'vue'
import {dayjs} from "element-plus";
import router from "@/router";

let getStuId = JSON.parse(localStorage.getItem('studentId')).toPrecision(18)
// console.log(getStuId)
let data = ref()
let messageId = ref()
// let isReadList = ref()
//获取列表
getMessage(getStuId).then((item) => {
    if (item.code == 200) {
        console.log(item)
        data.value = item.rows
        messageId.value = item.rows.map((val)=>{
            return val.psyMessages[0].id
        })
        // isReadList.value = item.rows.map((val)=>{
        //     return val.isRead
        // })
        // console.log(messageId.value)
        // console.log(Array.isArray(isReadList.value))
    }
})
//传递id
function getMessageId(index:number){
    // console.log(messageId.value[index])
    //判断是否已读
    getRead([data.value[index].id]).then((item)=>{
        console.log(item)
    })
    router.push({path:'/messageDetails',query:{messageId:messageId.value[index]}})
}


</script>

<template>
    <div class="box">
        <p>
            <router-link to="/personal">
                <van-icon name="arrow-left" class="toleft"/>
            </router-link>
            <span>消息</span>
        </p>
        <div class="infoMessage" v-for="(item,index) in data" :key="item.messageId" @click="getMessageId(index)">
            <div class="info">
                <p class="name">{{ item.psyMessages[0].title }}</p>
                <p>{{ item.psyMessages[0].content }}</p>
                <span>{{ dayjs(item.psyMessages[0].sendTime).format('YYYY-MM-DD') }}</span>
            </div>
            <div class="circle" v-if="item.isRead === '0'"></div>
        </div>
    </div>
</template>

<style scoped lang="less">
.box {
  min-height: 100vh;
  background-color: #F8F8F8;

  p {
    //margin-left: 10px;
    padding: 20px 15px;
    margin-bottom: 10px;
    position: relative;
    background-color: white;

    .toleft {
      font-size: 22px;
    }

    span {
      position: absolute;
      left: 45px;
      font-size: 18px;
    }
  }

  .infoMessage {
    position: relative;
    display: flex;
    margin: 15px auto;
    padding: 15px 15px 5px;
    width: 95vw;
    border-radius: 5px;
    background-color: white;

    .imgPic {
      width: 35px;
      height: 35px;
      border-radius: 50%;
      border: 1px solid black;
    }

    .info {
      position: relative;
      margin-left: 10px;
      width: 100%;

      .name {
        padding: 0;
      }

      p {
        padding: 0;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1; /* 显示3行 */
        overflow: hidden;
        text-overflow: ellipsis; /* 可选，用来在文本末尾显示省略号 */
      }

      span {
        position: absolute;
        top: 0;
        right: 0;
        color: #999999;
        font-size: 14px;
      }
    }

    .circle {
      position: absolute;
      top: 1px;
      right: 1px;
      width: 10px;
      height: 10px;
      border-radius: 50%;
      background-color: #FF1B1B;
    }
  }

}
</style>